package cn.skillith.showcompanybackend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/db")
public class DatabaseController {
    
    @Autowired
    private DataSource dataSource;
    
    @GetMapping("/test")
    public Map<String, Object> testDatabase() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            Connection connection = dataSource.getConnection();
            response.put("status", "SUCCESS");
            response.put("message", "数据库连接成功");
            response.put("database", connection.getCatalog());
            response.put("url", connection.getMetaData().getURL());
            connection.close();
        } catch (SQLException e) {
            response.put("status", "ERROR");
            response.put("message", "数据库连接失败: " + e.getMessage());
            response.put("errorCode", e.getErrorCode());
            response.put("sqlState", e.getSQLState());
        }
        
        return response;
    }
}
